########
# What does this do?
#######
rm(list=ls())

# load libraries
packs = c('tidyverse',
          'hrbrthemes', 'stargazer')
source("r/LoadPkg.R")
loadPkg(packs)

# read data
covars = read_csv(here('repFile', 'data', 'covars_final.csv'))

# dummy uribe
covars$uribe = ifelse(covars$uribe_feel_fix == 'Favorable', 1, 0)
covars$santos = ifelse(covars$santos_feel_fix == 'Favorable', 1, 0)


## convert factors to numeric for ease
mdata = 
  covars %>% 
  filter(nse != 'No aplica (Sin estratificación)')


# Convert to numeric
mdata$nse = 
  mdata$nse %>% 
  as_factor() %>% 
  fct_relevel("Estrato 1", "Estrato 2", 
              "Estrato 3", "Estrato 4",
              "Estrato 5", "Estrato 6") %>% 
  as.numeric()

mdata$age_factor = 
  mdata$age_factor %>% 
  as_factor() %>% 
  fct_relevel("De 18 a 25 años", 
              "De 26 a 35 años",
              "De 36 a 45 años",
              "De 46 a 55 años",
              "De 56 a 65 años",
              "Más de 65 años") %>% 
  as.numeric()


# voted at all
m1 = glm(voted_5_years_num ~ rearing_agg + nse + age_factor + 
      victim_num + woman, data = mdata, 
      family = binomial(link = 'logit'))
summary(m1)


stargazer(m1, 
          type = 'latex',
          label = 'tab:auth-vote5', 
          out = here("repFile", "paper", "figures", "auth-vote5.tex"),
          intercept.bottom = F, 
          covariate.labels = c('Intercept', 'Child-rearing', 
                                          'NSE Income', 'Age', 
                                          'Victim', 'Woman'), 
          keep.stat = 'n',
          title = 'Logit model of whether respondent voted in prior election.',
          dep.var.labels = 'Voted in last 5 years (yes/no)')

stargazer(m1, 
          type = 'latex',
          label = 'tab:auth-vote5', 
          out = here("repFile", "JOP submission", "auth-vote5.tex"),
          intercept.bottom = F, 
          covariate.labels = c('Intercept', 'Child-rearing', 
                               'NSE Income', 'Age', 
                               'Victim', 'Woman'), 
          keep.stat = 'n',
          title = 'Logit model of whether respondent voted in prior election.',
          dep.var.labels = 'Voted in last 5 years (yes/no)')
